const { isObject } = require('../utils/util')
class Order {
  constructor({
    id, senderName, senderPhone, senderAddress, 
    receiverName, receiverPhone, receiverAddress, 
    state, code
  }) {
    this.id = id || -1
    this.senderName = senderName || ''
    this.senderPhone = senderPhone || 0
    this.senderAddress = senderAddress || ''
    this.receiverName = receiverName || ''
    this.receiverPhone = receiverPhone || 0
    this.receiverAddress = receiverAddress || ''
    this.state = state || 1
    this.code = code || Date.now()
  }
}

const normalizeOrder = (data) => {
  if (Array.isArray(data)) {
    data = data.map(order => normalizeOrder(order))
  } 
  else if (isObject(data) && !(data instanceof Order)) {
    data = new Order({
      id: data.id,
      senderName: data.sender_name,
      senderPhone: data.sender_phone,
      senderAddress: data.sender_address,
      receiverName: data.receiver_name,
      receiverPhone: data.receiver_phone,
      receiverAddress: data.receiver_address,
      state: data.state,
      code: data.code
    })
  }
  return data
}

module.exports = { 
  Order,
  normalizeOrder
}